package per.hx.bs.wx.serviceimpl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.entity.WxUserDO;
import per.hx.bs.wx.mapper.WxUserMapper;

/**
 * @author HuXing
 * @date 2018/5/11  1:30
 */
@RestController
@RequestMapping(value = "/wxUser")
public class WxUserServiceImpl {

    private final WxUserMapper userMapper;

    @Autowired
    public WxUserServiceImpl(WxUserMapper userMapper) {
        this.userMapper = userMapper;
    }


    /**
     * 获取微信用户信息
     *
     * @param id id
     * @return 微信用户信息
     */
    @GetMapping(value = "/{id}")
    public WxUserDO get(@PathVariable("id") String id) {
        return userMapper.selectById(id);
    }

    /**
     * 获取微信用户信息
     *
     * @param openId id
     * @return 微信用户信息
     */
    @GetMapping(value = "/id/{openId}")
    public WxUserDO getByOpenId(@PathVariable("openId") String openId) {
        return userMapper.selectOne(new WxUserDO(openId));
    }

    /**
     * 插入信息
     *
     * @param wxUserDO 用户信息
     * @return 结果
     */
    @PostMapping
    public Integer insert(@RequestBody WxUserDO wxUserDO) {
        return userMapper.insert(wxUserDO);
    }

    /***
     *更新用户信息
     * @param wxUserDO 用户信息
     * @return 结果
     */
    @PutMapping
    public Integer update(@RequestBody WxUserDO wxUserDO) {
        EntityWrapper<WxUserDO> ew = new EntityWrapper<WxUserDO>();
        ew.eq("open_id", wxUserDO.getOpenId());
        return userMapper.update(wxUserDO, ew);
    }

    /**
     * 删除信息
     *
     * @param id 表id
     * @return 结果
     */
    @DeleteMapping(value = "/wxUser/{id}")
    public Integer delete(@PathVariable("id") String id) {
        return userMapper.deleteById(id);
    }

    /**
     * 删除信息
     *
     * @param openId 微信id
     * @return 结果
     */
    @DeleteMapping(value = "/wxUser/id/{openId}")
    public Integer deleteByOpenId(@PathVariable("openId") String openId) {
        EntityWrapper<WxUserDO> ew = new EntityWrapper<WxUserDO>();
        ew.eq("open_id", openId);
        return userMapper.delete(ew);
    }
}
